//
//  BaseNavigationController.swift
//  Browser
//
//  Created by 115 on 2017/9/5.
//  Copyright © 2017年 114la.com. All rights reserved.
//

import UIKit
@_exported import SnapKit
@_exported import YYKit
@_exported import SwiftyJSON



protocol BaseNavigationProtocol: NSObjectProtocol {
    
    /// 是否可以点击返回按钮pop
    func stopClickBackToPop()->Bool
    
}

class BaseNavigationController: UINavigationController {

    var fullScreenGe: UIPanGestureRecognizer!
    
    var canDragBack = true
    
    lazy var redImg: UIImage = {
        let color = UIColor.red
        let img = UIImage.init(color: color)
        return img!
    }()
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
//        self.navigationBar.setBackgroundImage(redImg, for: .default)
        self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.Text.X333333,
                                                  NSAttributedString.Key.font:UIFont.systemFont(ofSize: 18, weight: .medium)]
//        self.navigationBar.shadowImage = UIImage.init()
        self.navigationBar.isTranslucent = false
        
        let target = self.interactivePopGestureRecognizer?.delegate

        let sel = NSSelectorFromString("handleNavigationTransition:")

        let targetView = self.interactivePopGestureRecognizer?.view

        fullScreenGe = UIPanGestureRecognizer(target: target, action: sel)

        fullScreenGe.delegate = self

        targetView?.addGestureRecognizer(fullScreenGe)

        self.interactivePopGestureRecognizer?.isEnabled = false
        
    }
    
    let backImage = UIImage(named: "navi_back_dark")!
    
    override func didReceiveMemoryWarning() {
        
        super.didReceiveMemoryWarning()
        self.popToRootViewController(animated: true)
    }
    
    @objc func myPopVC() {
        
        if let topVC = self.topViewController as? BaseNavigationProtocol , topVC.stopClickBackToPop(){
            YRLog("该VC禁止了点击返回推出", type: .info)
        }else{
            self.popViewController(animated: true)
        }
        
    }
    
    @objc func myDismissVC() {
     
        self.dismiss(animated: true, completion: nil)
    }
    
    /// push一个控制器
    override func pushViewController(_ viewController: UIViewController,
                                     animated: Bool) {
        
        if viewController.navigationItem.leftBarButtonItem == nil && self.viewControllers.count > 0 {
            
            viewController.navigationItem.leftBarButtonItem = createNavBackButton()
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    
    /// 创建统一风格的返回按钮
    func createNavBackButton() -> UIBarButtonItem {
        let btn = UIButton.init(type: .custom)
        btn.frame = CGRect.init(x: 0, y: 0, width: 42, height: 42)
        btn.contentHorizontalAlignment = .left
        btn.setImage(backImage, for: .normal)
        btn.imageView?.contentMode = .scaleAspectFit
        btn.addTarget(self, action: #selector(myPopVC), for: .touchUpInside)
        let item = UIBarButtonItem.init(customView: btn)
        return item
    }
    
    /// 创建一个间隔修正
    func createFixSpaceItem() -> UIBarButtonItem{
        
        let item = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: nil, action: nil)
        item.width = -55.0
        return item
    }
    
    /// 创建统一风格的返回按钮
    func createNavCancleButton() -> UIBarButtonItem {
        
        let btn = UIButton.init(type: .custom)
        btn.frame = CGRect.init(x: 0, y: 0, width: 42, height: 42)
        btn.setTitle("取消", for: .normal)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.addTarget(self, action: #selector(myDismissVC), for: .touchUpInside)
        let item = UIBarButtonItem.init(customView: btn)
        return item
    }
    
    /// 插入一个子节点
    func insert(_ viewController: UIViewController,
                at index:Int) {
        
//        let fixed = createFixSpaceItem()
//        let back = createNavBackButton()
//        viewController.navigationItem.leftBarButtonItems = [fixed,back]
        
        viewController.navigationItem.leftBarButtonItem = createNavBackButton()
        self.viewControllers.insert(viewController, at: index)
    }
    
    /// 添加一个子节点
    func append(_ viewController: UIViewController) {
        
//        let fixed = createFixSpaceItem()
//        let back = createNavBackButton()
//        viewController.navigationItem.leftBarButtonItems = [fixed,back]
        viewController.navigationItem.leftBarButtonItem = createNavBackButton()
        self.viewControllers.append(viewController)
    }
    
    
//    override var preferredStatusBarStyle: UIStatusBarStyle{
//        return .lightContent
//    }
}

extension BaseNavigationController: UIGestureRecognizerDelegate {
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        guard canDragBack == true else {
            return false
        }
        
        if let topVC = self.topViewController as? BaseNavigationProtocol , topVC.stopClickBackToPop(){
            return false
        }
        
        guard let gestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer else { return false }
        let translation = gestureRecognizer.translation(in: gestureRecognizer.view)
        if translation.x <= 0 { return false }
        return self.children.count == 1 ? false : true
    }
}

